import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "@/views/HomeView";
import store from "@/store";
import message from "@/utils/message";
import  cookie  from "vue-cookies";

Vue.use(VueRouter);

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const originalPush2 = VueRouter.prototype.push;
VueRouter.prototype.replace = function replace(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: HomeView,
  },
  {
    path: "/find",
    component: () => import("../views/FindView.vue"),
  },
  {
    path: "/market",
    component: () => import("../views/MarketView.vue"),
  },
  {
    path: "/notice",
    component: () => import("../views/NoticesView.vue"),
  },
  {
    path: "/notice/detail",
    component: () => import("../components/NoticeDetail.vue"),
  },
  {
    path: "/news",
    component: () => import("../views/NewsView.vue"),
  },
  {
    path: "/news/detail",
    component: () => import("../components/NewsDetail.vue"),
  },
  {
    path: "/mine",
    component: () => import("../views/MineView.vue"),
    children: [
      {
        path: "/",
        component: () => import("../components/Mine.vue"),
      },
      {
        path: "mynft",
        component: () => import("../components/MyNft.vue"),
      },
      {
        path: "item",
        component: () => import("../components/MyItem.vue"),
      },
      {
        path: "buy",
        component: () => import("../components/BuyOrders.vue"),
      },
      {
        path: "sell",
        component: () => import("../components/SellOrder.vue"),
      },
      {
        path: "paymant",
        component: () => import("../components/Payment.vue"),
      },
      {
        path: "setting",
        component: () => import("../components/Setting.vue"),
        redirect: "/mine/setting/basic",
        children: [
          {
            path: "basic",
            component: () => import("../components/MineBasic.vue"),
          },
          {
            path: "safe",
            component: () => import("../components/MineSafes.vue"),
          },
          {
            path: "address",
            component: () => import("../components/MineAddress.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/item",
    component: () => import("../components/Item.vue"),
  },
  {
    path: "/group",
    component: () => import("../components/GoodsGroup.vue"),
  },
  {
    path: "/buyItem",
    component: () => import("../components/BuyView.vue"),
  },
  {
    path: "/login",
    component: () => import("../components/Login.vue"),
  },
  {
    path: "/register",
    component: () => import("../components/Registe.vue"),
  },
  // {
  //   path:'*',
  //   component: () => import('../components/404.vue')
  // }
];

// 路由自动登录

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  //获取跳转路径
  let toNema = to.name;
  //获取当前请求路径
  let toPath = to.path;
  if (toPath.indexOf("mine") === 1) {
    //获取token,cookie 中的token
    let token = cookie.get("token")
    //存储token
    if (token != undefined) {
      store.commit("setUserToken",token);
    }
  }
  //获取用户token
  let isLogin = store.state.token;
  if ( toPath.indexOf("mine") == -1) {
    console.log("无需登录");
    next({ replace: true });
  } else if (!isLogin && toPath.indexOf("mine") == 1) {
    console.log("需要登录"+isLogin);
    message.promptBox("用户未登录", "error");
    next({
      path: "/login",
    });
  } else if (isLogin && toPath.indexOf("mine") == 1) {
    console.log("可以访问")
    next({ replace: true });
  }

});
export default router;
